package jdbc;

import javafx.scene.Parent;

import java.sql.Connection;
import java.sql.ParameterMetaData;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.util.Scanner;

public class JDBCDemo9 {
    public static void main(String[] args) {
        /*try(Connection connection = DBUtil.getConnection()) {
            connection.setAutoCommit(false);
            String sql = "UPDATE user\n"+
                    "SET password = ?,nickname = ?,age = ?\n"+
                    "WHERE id = ?";
            PreparedStatement ps = connection.prepareStatement(sql);
            ps.setString(1,"666666");
            ps.setString(2,"大奇");
            ps.setInt(3,36);
            ps.setInt(4,3);
            int num = ps.executeUpdate();
            System.out.println(num>0? "修改成功":"修改失败");

           *//* connection.rollback();
            System.out.println("回滚了,修改不作数");*//*

            connection.commit();
            System.out.println("提交了,修改成功");
        } catch (SQLException e) {
            e.printStackTrace();
        }*/



        try(Connection connection =DBUtil.getConnection()) {
            connection.setAutoCommit(false);
            String sql = "UPDATE user\n"+
                    "SET password = ?,nickname = ?,age = ?\n"+
                    "WHERE id = ?";
            PreparedStatement ps = connection.prepareStatement(sql);
            ps.setString(1,"223456");
            ps.setString(2,"大奇奇");
            ps.setInt(3,40);
            ps.setInt(4,2);
            int num = ps.executeUpdate();
            System.out.println(num>0? "修改成功":"修改失败");

            Scanner scanner = new Scanner(System.in);
            System.out.println("请输入rollback或commit");
            String s = scanner.nextLine();
            switch (s){
                case "rollback":
                    connection.rollback();
                    System.out.println("回滚了,修改不作数");
                    break;
                case "commit":
                    connection.commit();
                    System.out.println("提交了,修改成功");
                    break;
                default:
                    System.out.println("输入有误");
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }
}
